// 2. 主模块内，配置小模块的基目录
require.config({
    baseUrl: "./modules",
    paths: {
        jq: "../libs/jquery",
        getData: "getData",
        render: "indexRender",
        addList: "addList"
    }
})

// 3. 主模块内，引入具体的小模块
require(["jq", "getData", "render", "addList"], function (_, getData, render, addList) {
    // console.log($)

 // 首页的登录守卫
 if(localStorage.getItem("token") === "ok"){
    $(".p1").hide()
    
    $(".p2").show().children("span").html(localStorage.getItem("un"))
}

// 退出功能
$(".exit").click(function(){
    localStorage.setItem("token","");
    $(".p1").show()
    $(".p2").hide()
})


    /* 登录守卫 */
    var goCart = document.getElementById("goCart");
    console.log(goCart);
    // 去购物车的登录守卫
    goCart.onclick = function () {
        if (localStorage.getItem("token") !== "ok") {
            if (confirm("您好，请您先登录")) {
                location.href = "./login.html"
            }
            return false;
        }
    }

    var wrap = document.querySelector(".wrap");
    var next = document.querySelector(".arrow_right");
    var prev = document.querySelector(".arrow_left");
    next.onclick = function () {
        next_pic();
    }
    prev.onclick = function () {
        prev_pic();
    }
    function next_pic () {
        index++;
        if(index > 4){
            index = 0;
        }
        showCurrentDot();
        var newLeft;
        if(wrap.style.left === "-7680px"){
            newLeft = -2560;
        }else{
            newLeft = parseInt(wrap.style.left)-1280;
        }
        wrap.style.left = newLeft + "px";
    }
    function prev_pic () {
        index--;
        if(index < 0){
            index = 4;
        }
        showCurrentDot();
        var newLeft;
        if(wrap.style.left === "0px"){
            newLeft = -5120;
        }else{
            newLeft = parseInt(wrap.style.left)+1280;
        }
        wrap.style.left = newLeft + "px";
    }
    var timer = null;
    function autoPlay () {
        timer = setInterval(function () {
            next_pic();
        },2000);
    }
    autoPlay();

    var container = document.querySelector(".container");
    container.onmouseenter = function () {
        clearInterval(timer);
    }
    container.onmouseleave = function () {
        autoPlay();    
    }

    var index = 0;
    var buttons = document.querySelector(".buttons");
    var dots = buttons.getElementsByTagName("span");
    function showCurrentDot () {
        for(var i = 0, len = dots.length; i < len; i++){
            dots[i].className = "";
        }
        dots[index].className = "on";
    }

    for (var i = 0, len = dots.length; i < len; i++){
        (function(i){
            dots[i].onclick = function () {
                var dis = index - i;
                if(index == 4 && parseInt(wrap.style.left)!==-6400){
                    dis = dis - 5;     
                }
                //和使用prev和next相同，在最开始的照片5和最终的照片1在使用时会出现问题，导致符号和位数的出错，做相应地处理即可
                if(index == 0 && parseInt(wrap.style.left)!== -1280){
                    dis = 5 + dis;
                }
                wrap.style.left = (parseInt(wrap.style.left) +  dis * 1280)+"px";
                index = i;
                showCurrentDot();
            }
        })(i);            
    }

    
    const url = "http://localhost:3000/api?type=getGoods";
    // 4. 引入成功之后，按需执行小模块
    getData(url).then((res) => {
        // console.log(res)
        // 将指定数据，渲染到指定容器
        render(res, $("#cont"));

        // 绑定事件
        $("#cont").on("click", ".add", function () {
            // console.log($(this).parent().attr("id"))
            // 准备存储localStorage数据
            addList($(this));
        })
    })
})